package nci60.data.maxquant.peptide
import nci60.data.protein.Protein
import nci60.data.maxquant.Project
import nci60.data.maxquant.msms.MsMs
import nci60.data.maxquant.Experiment

/**
 * General information about the identified peptides (source: peptides.txt)
 *
 * @autor <a href="mailto:Florian.J.Auer@googlemail.com">Florian J. Auer</a>
*/
class Peptide {
    
    /**
    * Amino acid sequence of the peptide
    */
    String sequence
    
    /**
    * Number of missed Cleavages in this peptide
    */
    Integer missedCleavages
    
    /**
    * Monoisotopic mass (Da) of the peptide 
    */
    Double mass
    
    /**
    * Best scoring protein this peptide is associated with
    * @see nci60.data.protein.Protein
    */
    Protein leadingRazorProtein
    
    /**
    * This particular peptide is unique to a single protein group in the proteinGroups file
    */
    boolean uniqueGroups
    
    /**
    * This particular peptide is unique to a single protein sequence in the fasta file(s)
    */
    boolean uniqueProteins
    
    /**
    * Posterior Error Probability of the identification. This value essentially operates as a p-value, where smaller is better
    */
    Double pep
    
    /**
    * Andromeda score for the best associated MS/MS spectrum
    */
    Double score
    
    /**
    * Summed up eXtracted Ion Current (XIC) of all isotopic clusters associated with the identified AA sequence
    */
    Double intensity
    
    /**
    * When true, this particular peptide was found to be part of a protein derived from the reversed part of the decoy database. These should be removed for further data analysis
    */
    boolean reverse
    
    /**
    * When true, this particular peptide was found to be part of a commonly occurring contaminant. These should be removed for further data analysis
    */
    boolean contaminant
    
    /**
    * id used in MaxQuant output file
    */
    String idInFile
    
    /**
    * reference to the analysis it belongs to <p />
    * @see nci60.data.maxquant.Project
    */
    Project currentProject

    
    
    /**
     * GRAILS <a href="http://grails.org/doc/latest/guide/GORM.html#gormAssociation" target="_blank">Associations in Gorm</a>
     * <p />
     * <style>.custom-table td{border-top:1pt solid grey;vertical-align:top;} .custom-table{border-collapse:collapse;width:100%;}</style>
     * <table class="custom-table">
     * <tr><td>
     *              charges
     * </td><td>        All charge states that have been observed <p />
     *                  <b>See Also:</b> {@link nci60.data.maxquant.peptide.PeptideCharge} <p />
     *                  The relationship has a customized mapping as jointable "peptide_charges" (many-to-many relationship)<p />
     *                  <b>See Also:</b> {@link #mapping}
     * </td></tr>
     * <tr><td>
     *              experiments
     * </td><td>        Peptide information specific for each cell line (experiment) <p />
     *                  <b>See Also:</b> {@link nci60.data.maxquant.peptide.PeptideExperiment} <p />
     * </td></tr>
     * <tr><td>
     *              msms
     * </td><td>        The MS/MS scans identifying this peptide<p />
     *                  <b>See Also:</b> {@link nci60.data.maxquant.msms.MsMs} <p />
     * </td></tr>
     * </table>
     */
    static hasMany = [charges:PeptideCharge,
                      experiments:PeptideExperiment,
                      msms:MsMs]

    /**
     * GRAILS <a href="http://grails.org/doc/latest/guide/single.html#constraints" target="_blank">Declaring Constraints</a>
     * <p />
     * Constraints in Grails are a way to declaratively specify validation rules for generating objects in GORM.
     */	
    static constraints = {
        leadingRazorProtein nullable: true
    }

    /**
     * GRAILS <a href="http://grails.org/doc/latest/guide/GORM.html#ormdsl" target="_blank">Custom ORM Mapping</a>
     * <p />
     * Grails domain classes can be mapped onto many legacy schemas with an Object Relational Mapping DSL (domain specific language). 
     */	
    static mapping = {
        version false
        charges joinTable: 'peptide_charges'
    }

    /**
     * Transforms the attribute values from the MaxQuant input file to values suitable for sql. e.g. "+", and "", are converted into "true", and "false" respectively. <p />
     * This mehtod is used while importing new data.
     * 
     * @param property  name of the property of this class (specified above)
     * @param input     attribute value in the MaxQuant output file
     * @return  replaced sql value of the input
     */		
    static String modifySQL(String property, String input) {
        String output
        if((property=='reverse')||(property=='contaminant')){
            if(input=='+'){
                output = '1'
            }else{
                output = '0'
            }
        }else if((property=='uniqueGroups')||(property=='uniqueProteins')){
            if(input=='yes'){
                output = '1'
            }else{
                output = '0'
            }
        }else if((property=='sequence')){
            output = '\"'+input+'\"'
        }else{
            output = input
        }
        return output
    }
}
